package com.s7.widget.admix;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * 图文混合输入EditText
 */
public class AdmixEdit extends RecyclerView {

    private AdmixAdapter mAdapter;

    public AdmixEdit(@NonNull Context context) {
        this(context, null);
    }

    public AdmixEdit(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AdmixEdit(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (mAdapter == null) {
            mAdapter = new AdmixAdapter(context);
        }
        setLayoutManager(new LinearLayoutManager(context));
        setAdapter(mAdapter);
    }

    /**
     * 添加网络图片
     * @param url
     */
    public void setImage(String url) {
        mAdapter.addImage(url);
    }

    /**
     * 添加文字
     * @param end
     */
    public void append(String end) {
        mAdapter.append(end);
    }

    /**
     * 设置数据
     * @param datas
     */
    public void setDatas(List<AdmixData> datas) {
        if (!TextUtils.isEmpty(datas.get(datas.size() - 1).getUrl())) {
            datas.add(new AdmixData());
        }
        mAdapter.setDatas(datas);
    }

    /**
     * 获取数据
     * @return
     */
    public List<AdmixData> getDatas() {
        return mAdapter.getDatas();
    }

    /**
     * 刷新数据
     */
    public void notifyDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }
}
